package cn.yr.clock.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.yr.clock.domain.entity.User;
import cn.yr.clock.domain.vo.AuthVo;
import cn.yr.clock.domain.vo.PageVo;
import cn.yr.clock.domain.vo.UserVo;
import cn.yr.clock.handler.AppletHandler;
import cn.yr.clock.handler.EngineHandler;
import cn.yr.clock.service.UserService;
import cn.yr.common.exception.ret.Ret;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 用户信息表
 * @author Lang
 * @email 1102076808@qq.com
 * @date 2021-09-25 11:13:41
 */
@RestController
@RequestMapping("/user")
public class UserController{
    @Autowired
    private UserService userService;

    @Autowired
    private AppletHandler appletHandler;

    @Autowired
    private EngineHandler engineHandler;

    @GetMapping("/getWechatOpenId")
    public Ret<Object> getOpenId(String code){
        String openId = appletHandler.getOpenId(code);
        if (StrUtil.isNotEmpty(openId)) {
            return Ret.success(MapUtil.builder("openid",openId).map());
        } else {
            return Ret.error("未能获取到openid");
        }
    }

    @PostMapping("/wechatLogin")
    public Ret<AuthVo> wechatLogin(@RequestBody User user) {
        AuthVo authVo = userService.wechatLogin(user);
        return Ret.success(authVo);
    }

    @PostMapping("/camouflageLogin")
    public Ret<AuthVo> camouflageLogin(@RequestBody Long userId) {
        AuthVo authVo = userService.camouflageLogin(userId);
        return Ret.success(authVo);
    }

    /**
     * 列表
     */
    @RequestMapping("/page")
    public Ret<PageVo<User>> page(@RequestBody PageVo<User> pageVo){
    	PageVo<User> pageUser = userService.page(pageVo,new QueryWrapper<>());
        return Ret.success(pageUser);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Ret<List<UserVo>> list(){
        List<User> list = userService.list();
        List<UserVo> collect = list.stream().map(e -> {
            UserVo userVo = new UserVo();
            BeanUtil.copyProperties(e, userVo);
            userVo.setSumClock(engineHandler.sumClock(e.getUserId()));
            userVo.setContinuousClock(engineHandler.continuousClock(e.getUserId()));
            userVo.setCycleNum(engineHandler.cycleNum(e.getUserId()));
            return userVo;
        }).collect(Collectors.toList());
        return Ret.success(collect);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{userId}")
    public Ret<User> info(@PathVariable("userId") Long userId){
		User  user = userService.getById(userId);
	    return Ret.success(user);
    }

    /**
     * 保存
     */
    @RequestMapping("/add")
    public Ret<Object> save(@Validated @RequestBody User user){
		userService.save(user);
	    return Ret.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/edit")
    public Ret<Object> update(@Validated @RequestBody User user){
		userService.updateById(user);
	    return Ret.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/remove")
    public Ret<Object> delete(@RequestBody Long[] userIds){
		userService.removeByIds(Arrays.asList(userIds));
        return Ret.success();
    }

}
